package cn.bytecollege.service;

import cn.bytecollege.mapper.*;
import cn.bytecollege.model.*;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommunityService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper addressMapper;

    /**
     * 查询物资信息
     * @return
     */

    public List<Goods> getAll(){
        return goodsMapper.selectList(null);
    }

    /**
     * 添加物资
     * @param goods
     * @return
     */

    public int add(Goods goods){
        return goodsMapper.insert(goods);
    }
    //删除物资

    /**
     * 删除物资
     * @param id
     * @return
     */

    public int delById(Integer id){
        return goodsMapper.deleteById(id);
    }


    /**
     * 根据id查询物资信息
     * @param id
     * @return
     */

    public Goods getById(Integer id){
        return goodsMapper.selectById(id);
    }
    public User getUserById(Integer id){
       return userMapper.selectById(id);
    }
    public Orders getByOrders(Integer id){
        Wrapper<Orders> ordersWrapper = new QueryWrapper<Orders>().eq("order_id",id).select("order_id","user_id","send_user_id","order_state","order_date","user_address_id","order_check");
        return orderMapper.selectOne(ordersWrapper);
    }

    /**
     * 修改物资信息
     * @param goods
     * @return
     */
    public int modById(Goods goods){
        return goodsMapper.updateById(goods);
    }
    public int modByOrderId(Orders orders){
        return orderMapper.updateById(orders);
    }



    /**
     * 查询类型为食品的物资
     * @param start
     * @param size
     * @return
     */

    public Page<Goods> foodList(int start,int size){
        Page<Goods> page=new Page<>(start,size);
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>().eq("goods_kind","食品");
        goodsMapper.selectPage(page, wrapper);
        return page;
    }


    /**
     * 查询类型为生活用品的物资
     * @param start
     * @param size
     * @return
     */

    public Page<Goods> lifeList(int start,int size){
        Page<Goods> page=new Page<>(start,size);
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>().eq("goods_kind","生活用品");
        goodsMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 查询类型为衣物的物资
     * @param start
     * @param size
     * @return
     */

    public Page<Goods> clothList(int start,int size){
        Page<Goods> page=new Page<>(start,size);
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>().eq("goods_kind","衣物");
        goodsMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 根据日期查询订单信息
     * @return
     */
    public Page<Orders> ordersList(Date date,int start,int size){
        Page<Orders> page=new Page<>(start,size);
        Wrapper<Orders> ordersWrapper = new QueryWrapper<Orders>().eq("order_date",date).select("order_id","user_id","send_user_id","order_state","order_date","user_address_id","order_check");
        Page<Orders> ordersPage = orderMapper.selectPage(page, ordersWrapper);
        List<Orders> ordersList = ordersPage.getRecords();
        for (Orders orders:ordersList){
            Integer userAddressId = orders.getUserAddressId();
            orders.setUserAddress(addressMapper.selectById(userAddressId));
            Integer orderId = orders.getOrderId();
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id",orderId).select("order_detail_id","order_id","goods_id","goods_price","goods_number");
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(detailWrapper);
            for (OrderDetail orderDetail:orderDetails){
                Integer goodsId = orderDetail.getGoodsId();
                orders.setGoods(goodsMapper.selectById(goodsId));
            }
        }
        return ordersPage;
    }

    /**
     * 查询全部的订单
     * @param start
     * @param size
     * @return
     */
    public Page<Orders> ordersList(int start,int size){
        Wrapper<Orders> ordersWrapper = new QueryWrapper<Orders>().select("order_id","user_id","send_user_id","order_state","order_date","user_address_id","order_check");
        Page<Orders> page=new Page<>(start,size);
        Page<Orders> ordersPage = orderMapper.selectPage(page, ordersWrapper);
        List<Orders> ordersList = ordersPage.getRecords();
        for (Orders orders:ordersList){
            Integer userAddressId = orders.getUserAddressId();
            orders.setUserAddress(addressMapper.selectById(userAddressId));
            Integer sendUserId = orders.getSendUserId();
            System.out.println(sendUserId);
            Wrapper<User> userWrapper = new QueryWrapper<User>().eq("user_id",sendUserId).select("user_id","username","password","power","evaluation_power");
            orders.setUser(userMapper.selectOne(userWrapper));
            Integer orderId = orders.getOrderId();
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id",orderId).select("order_detail_id","order_id","goods_id","goods_price","goods_number");
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(detailWrapper);
            for (OrderDetail orderDetail:orderDetails){
                Integer goodsId = orderDetail.getGoodsId();
                orders.setGoods(goodsMapper.selectById(goodsId));
            }
        }
        return page;

    }

    /**
     * 分配订单
     * @return
     */

    public Page<Orders> addTask(int start,int size){
        Wrapper<Orders> ordersWrapper = new QueryWrapper<Orders>().eq("order_state","待发货").select("order_id","user_id","send_user_id","order_state","order_date","user_address_id","order_check");
        Page<Orders> page=new Page<>(start,size);
        Page<Orders> ordersPage = orderMapper.selectPage(page, ordersWrapper);
        List<Orders> ordersList = ordersPage.getRecords();
        for (Orders orders:ordersList){
            Integer userAddressId = orders.getUserAddressId();
            orders.setUserAddress(addressMapper.selectById(userAddressId));
            Integer sendUserId = orders.getSendUserId();
            System.out.println(sendUserId);
            Wrapper<User> userWrapper = new QueryWrapper<User>().eq("user_id",sendUserId).select("user_id","username","password","power","evaluation_power");
            orders.setUser(userMapper.selectOne(userWrapper));
            Integer orderId = orders.getOrderId();
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id",orderId).select("order_detail_id","order_id","goods_id","goods_price","goods_number");
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(detailWrapper);
            for (OrderDetail orderDetail:orderDetails){
                Integer goodsId = orderDetail.getGoodsId();
                orders.setGoods(goodsMapper.selectById(goodsId));
            }
        }
        return page;
    }
    public int addOrder(Orders orders){
        return orderMapper.updateById(orders);
    }



    public Page<Goods> getByPage(String name,int start, int size) {
        Wrapper<Goods> goodsWrapper = new QueryWrapper<Goods>().like("goods_name",name).select();
        Page<Goods> page=new Page<>(start,size);
        goodsMapper.selectPage(page, goodsWrapper);
        return page;
    }
    }

